﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NomClasses;

namespace NomCafe
{
    public partial class frmDiscountManager : DevComponents.DotNetBar.Office2007RibbonForm
    {
        string nameDiscount;
        public frmDiscountManager()
        {
            InitializeComponent();
        }

        private void frmDiscountManager_Load(object sender, EventArgs e)
        {
            getDataDiscount();
        }

        private void getDataDiscount()
        {
            listViewDiscount.Items.Clear();
            // Lấy danh sách chiết khấu cho listViewMode            
            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_discount> discountList = nc.nom_discounts;
            foreach (NomClasses.nom_discount discount in discountList)
            {
                string[] row = {discount.discount.ToString() , discount.time_begin.ToString() , 
                                discount.time_end.ToString() , discount.note};
                listViewDiscount.Items.Add(discount.name).SubItems.AddRange(row);
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                if (txtNameDiscount.Text != "" && inputDateBegin.Text != "" && inputDateEnd.Text != "" &&
                    inputPercent.Text != "")
                {
                    NomDataContext nc = new NomDataContext();
                    var discountList = from discount in nc.nom_discounts
                                       select discount;
                    foreach (var i in discountList)
                    {
                        if (txtNameDiscount.Text.Trim().ToLower().Equals(i.name.Trim().ToLower()))
                        {
                            flag = true;
                        }
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_DISCOUNT_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        if (inputPercent.Value != 0)
                        {
                            if (inputDateBegin.Value >= inputDateEnd.Value)
                            {
                                MessageBox.Show(languageManager.DATETIME_DISCOUNT_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                inputDateBegin.Text = "";
                                inputDateEnd.Text = "";
                                inputDateBegin.Focus();
                            }
                            else
                            {
                                nom_discount discount = new nom_discount();
                                discount.name = txtNameDiscount.Text;
                                discount.time_begin = inputDateBegin.Value;
                                discount.time_end = inputDateEnd.Value;
                                discount.discount = inputPercent.Value;
                                discount.note = txtNoteDiscount.Text;
                                nc.nom_discounts.InsertOnSubmit(discount);
                                nc.SubmitChanges();

                                getDataDiscount();
                            }
                        }
                        else
                        {
                            MessageBox.Show(languageManager.PERCENT_DISCOUNT_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);                            
                            inputPercent.Focus();
                        }
                    }
                }
                else
                {
                    MessageBox.Show(languageManager.INSERT_DISCOUNT_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnRenew_Click(object sender, EventArgs e)
        {
            getDataDiscount();

            txtNameDiscount.Text = "";
            txtNoteDiscount.Text = "";
            inputDateBegin.Text = "";
            inputDateEnd.Text = "";
            inputPercent.Text = null;
            txtNameDiscount.Focus();

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnAccept.Enabled = true;            
        }

        private void listViewDiscount_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnAccept.Enabled = false;

            nameDiscount = "";
            for (int i = 0; i < listViewDiscount.Items.Count; i++)
            {
                if (listViewDiscount.Items[i].Selected)
                {
                    nameDiscount = listViewDiscount.Items[i].SubItems[0].Text;
                    txtNameDiscount.Text = nameDiscount;
                    inputPercent.Text = listViewDiscount.Items[i].SubItems[1].Text;
                    inputDateBegin.Text = listViewDiscount.Items[i].SubItems[2].Text;
                    inputDateEnd.Text = listViewDiscount.Items[i].SubItems[3].Text;
                    txtNoteDiscount.Text = listViewDiscount.Items[i].SubItems[4].Text;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                NomDataContext nc = new NomDataContext();
                nom_discount discount = nc.nom_discounts.Single(i => i.name == nameDiscount);
                nc.nom_discounts.DeleteOnSubmit(discount);
                nc.SubmitChanges();

                btnRenew_Click(sender, e);                                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                NomDataContext nc = new NomDataContext();
                var discountList = from discount in nc.nom_discounts
                                   where discount.name != nameDiscount
                                   select discount;
                    foreach (var i in discountList)
                    {
                        if (txtNameDiscount.Text.Trim().ToLower().Equals(i.name.Trim().ToLower()))
                        {
                            flag = true;
                        }
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_DISCOUNT_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        nom_discount discount = nc.nom_discounts.Single(i => i.name == nameDiscount);
                        discount.name = txtNameDiscount.Text;
                        discount.discount = inputPercent.Value;
                        discount.time_begin = inputDateBegin.Value;
                        discount.time_end = inputDateEnd.Value;
                        discount.note = txtNoteDiscount.Text;
                        nc.SubmitChanges();

                        btnRenew_Click(sender, e);
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }        
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
